﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index_CreateParty : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            insertNamesIntoList();
        }
    }

    private void insertNamesIntoList()
    {
        PartyFinderDBDataContext db = new PartyFinderDBDataContext();
        MembershipUser user = Membership.GetUser();

        var data = from u in db.aspnet_Users
                   where u.UserName == user.UserName
                   join f in db.Friends on u.UserId equals f.UserId
                   join uf in db.aspnet_Users on f.FriendsId equals uf.UserId
                   orderby uf.LastName
                   select new { name = (uf.UserName + " :  " + uf.Name + " " + uf.LastName), uf.UserId };

        ListBox1.DataSource = data;
        ListBox1.DataTextField = "name";
        ListBox1.DataValueField = "UserId";
        ListBox1.DataBind();
    }

    public ArrayList BoxList()
    {
        ArrayList boxlist = new ArrayList();
        foreach (ListItem lst in ListBox2.Items)
        {
            if (lst.Selected)
            {
                boxlist.Add(ListBox2.SelectedItem.ToString());
            }
        }
        return boxlist;
    }

    protected void Select_Click(object sender, EventArgs e)
    {
        bool flag = false;
        string selecteditem = ListBox1.SelectedItem.Text;
        if (ListBox2.Items.Count >= 1)
        {
            for (int i = 0; i < ListBox2.Items.Count; i++)
            {
                if (selecteditem == ListBox2.Items[i].Text)
                {
                    Response.Write("Already Item Selected");
                    flag = true;
                }
            }
            if (flag == false)
            {
                ListItem li = new ListItem();
                li.Text = selecteditem;
                li.Value = ListBox1.SelectedValue;
                ListBox2.Items.Add(li);
            }
        }
        else
        {
            ListItem li = new ListItem();
            li.Text = selecteditem;
            li.Value = ListBox1.SelectedValue;
            ListBox2.Items.Add(li);
        }
    }

    protected void SelectAll_Click(object sender, EventArgs e)
    {
        ListBox2.Items.Clear();
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = ListBox1.Items[i].Text;
            li.Value = ListBox1.Items[i].Value;
            ListBox2.Items.Add(li);
        }
    }

    protected void Remove_Click(object sender, EventArgs e)
    {
        try
        {
            if (ListBox1.Items.Count >= 1)
            {
                if (ListBox1.SelectedValue != null)
                {
                    string selecteditem2 = ListBox1.SelectedItem.Text;
                    ListBox2.Items.Remove(selecteditem2);
                }
            }
            else
            {
                Response.Write("No ITEMS Found");
            }
        }
        catch (Exception e1)
        {
            var Text = e1.Message;
        }
    }

    protected void RemoveAll_Click(object sender, EventArgs e)
    {
        ListBox2.Items.Clear();
    }

    protected void BtBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("HomePage.aspx");
    }

    protected void BtSubmit_Click(object sender, EventArgs e)
    {
        PartyFinderDBDataContext db = new PartyFinderDBDataContext();
        var userid = db.aspnet_Users.SingleOrDefault(x => x.UserId.Equals(Membership.GetUser().ProviderUserKey));
        Party party = new Party();


        party.UserId = userid.UserId;
        party.Tema = TxtTema.Text;
        party.Date = TxtDate.Text;
        party.Time = TxtTime.Text;
        party.Duration = Convert.ToInt32(TxtDuration.Text);
        party.Street = TxtStreet.Text;
        party.StreetNr = Convert.ToInt32(TxtStreetNr.Text);
        party.Department = TxtDepartment.Text;
        party.ZipCode = Convert.ToInt32(TxtZipCode.Text);
        party.Town = TxtTown.Text;
        party.Drink = CheckBoxDrink.Checked;
        party.Food = CheckBoxFood.Checked;
        party.Comment = TxtComment.Text;

        var partyId = (from p in db.Parties
                       select (p.PartyId)).Max();
        int pid = Convert.ToInt32(partyId.ToString())+1;
        party.PartyId = pid;

        db.Parties.InsertOnSubmit(party);
        db.SubmitChanges();

        for (int i = 0; i < ListBox2.Items.Count; i++)
        {
            string participantId = ListBox2.Items[i].Value.ToString();
            PartyParticipant participant = new PartyParticipant();
            Guid participantGuid = Guid.Empty;
            participantGuid = new Guid(participantId);

            participant.UserId = participantGuid;
            participant.PartyId = pid;
        db.PartyParticipants.InsertOnSubmit(participant);
        db.SubmitChanges();
        }
        Response.Redirect("HomePage.aspx");
    }
}